#include "mainwindow.h"
#include "ui_mainwindow.h"

ProjectData g_pro_data = {"format", 1920, 1080, ""};
bool g_preview = false;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),
    m_format(NULL),
    format_type("LCD"),
    m_project_full_path("")
{
    ui->setupUi(this);

    //暂时未知有何作用
    //installEventFilter(this);

    //先在designer里面设置左侧栏的最小值，再设置右侧无限延展
    ui->splitter->setStretchFactor(0, 0);
    ui->splitter->setStretchFactor(1, 1);

    //按钮 hide之后就不响应shortcut快捷键了,在ui中设置了shortcut，重写keypress相应保存数据槽函数
    ui->widget_button->hide();

    ui->statusBar->hide();

    //界面添加转到槽之后这句话还是要写，否则无响应
    connect(ui->action_help, SIGNAL(triggered()), this, SLOT(on_action_triggered_help()));
    connect(ui->action_save, SIGNAL(triggered()), this, SLOT(on_pushButton_save_clicked()));

    QString help_text = "";
    foreach (QString one, content_list)
    {
        help_text += one;
        help_text += QString("\n");
    }
    QString explain_text = "";
    foreach (QString one, explain_list)
    {
        explain_text += one;
        explain_text += QString("\n");
    }
    m_help_dialog = new HelpDialog(help_text, explain_text);
}

MainWindow::~MainWindow()
{
    if (m_format != NULL)
    {
        delete m_format;
        m_format = NULL;
    }

    if (m_help_dialog != NULL)
    {
        delete m_help_dialog;
        m_help_dialog = NULL;
    }

    delete ui;
}

//按钮槽函数
void MainWindow::on_pushButton_close_clicked()
{
    close();
}
void MainWindow::on_pushButton_create_clicked()
{
    CreateProjectDialog dialog;
    ProjectData pro_data = dialog.showAndGetResult();

    if (pro_data.name == "")
        return;

    g_pro_data = pro_data;

    m_project_path = pro_data.path;

    if (!pro_data.path.endsWith('/'))
        pro_data.path = pro_data.path + "/";

    m_project_full_path = pro_data.path + pro_data.name;

    //直接创建
    m_format = new Format();
    m_format->type = pro_data.type;
    m_format->initDeflultData(pro_data.width, pro_data.height);

    ui->stackedWidget->setCurrentIndex(page_edit);

    initTreeWidget(m_format);
    initTabWidget(m_format);
}
void MainWindow::on_pushButton_open_clicked()
{
    QString title = QObject::tr("please select a json file");
    m_project_full_path = QFileDialog::getOpenFileName(this, title, "", "Json File(*)");
    //项目文件夹包含文件名
    if (m_project_full_path == "")
        return;

    QFileInfo fileInfo = m_project_full_path;
    //项目文件夹不包含文件名   E:/dymap-edit/hang2
    m_project_path = fileInfo.path();

    //代理创建
    m_format = m_json_handle.createFormatData(m_project_full_path);
    g_pro_data.path = m_project_path;

    if (m_format == NULL)
    {
        QMessageBox::warning(this, QObject::tr("waring"), QObject::tr("project json file open failed"));
        return;
    }

    ui->stackedWidget->setCurrentIndex(page_edit);

    initTreeWidget(m_format);
    initTabWidget(m_format);
}
void MainWindow::on_pushButton_save_clicked()
{
    //按钮 hide之后就不响应shortcut快捷键了,在ui中设置了shortcut，重写keypress相应保存数据槽函数
    if (!m_format)
        return;
    ui->treeWidget->getAllDataFromTwView();

    QDateTime datetime = QDateTime::currentDateTime();
    m_format->architecture_version = datetime.toString("yyyyMMddhhmmss");

    m_json_handle.saveFormatData(m_format, m_project_full_path);
}

//tab->mainwindow->tree
void MainWindow::on_tabWidget_currentChanged(int index)
{
    //切换页面
    int screen_id = ui->tabWidget->getCurrentChangedScreenId(index);
    ui->treeWidget->setSelectedScreenItem(screen_id);
}
//选择控件
void MainWindow::on_item_select(int screen_id, int partition_id, int layout_id, int item_id, QString cab_pos_item_str)
{
    ui->treeWidget->setSelectedItemItem(screen_id, partition_id, layout_id, item_id, cab_pos_item_str);
}
void MainWindow::on_layout_select(int screen_id, int partition_id, int layout_id)
{
    ui->treeWidget->setSelectedLayoutItem(screen_id, partition_id, layout_id);
}
void MainWindow::on_partition_select(int screen_id, int partition_id)
{
    ui->treeWidget->setSelectedPartitionItem(screen_id, partition_id);
}
void MainWindow::on_screen_select(int screen_id)
{
    ui->treeWidget->setSelectedScreenItem(screen_id);
}


//tree->mainwindow->tab
void MainWindow::on_treeScreenSelected(int screen_id)
{
    //选择页面
    ui->tabWidget->setCurrentChangeedScreenFromId(screen_id);
}
//选择控件
void MainWindow::on_treePartitionSelected(int screen_id, int partition_id)
{
    ui->tabWidget->setPartitionSelected(screen_id, partition_id);
}
void MainWindow::on_treeLayoutSelected(int screen_id, int partition_id, int layout_id)
{
    ui->tabWidget->setLayoutSelected(screen_id, partition_id, layout_id);
}
void MainWindow::on_treeItemSelected(int screen_id, int partition_id,  int layout_id, int item_id, QString cab_pos_item_str)
{
    ui->tabWidget->setItemSelected(screen_id, partition_id, layout_id, item_id, cab_pos_item_str);
}
void MainWindow::on_addScreen()
{
    Format tmp_data;

    AddScreenDialog dialog;
    int result = dialog.showAndGetResult();

    if (result != 0)
    {
        if (m_format->isIdRepeated(result))
        {
            QMessageBox::warning(this, QObject::tr("waring"), QObject::tr("id repeated"));
        }
        else
        {
            addScreen(tmp_data.initScreen(result));
        }
    }
}
void MainWindow::on_delScreen(int screen_id)
{
    ui->tabWidget->delScreen(screen_id);
}
void MainWindow::on_addPartition(int screen_id)
{
    //选翻页
    ui->tabWidget->setCurrentChangeedScreenFromId(screen_id);
    Format tmp_format;
    addPartition(screen_id, tmp_format.initPartition(m_format->getNewPartitionId(screen_id)));
}
void MainWindow::on_delPartition(int screen_id, int partition_id)
{
    ui->tabWidget->delPartition(screen_id, partition_id);
}
void MainWindow::on_addLayout(int screen_id, int partition_id)
{
    ui->tabWidget->setCurrentChangeedScreenFromId(screen_id);
    Format tmp_data;
    addLayout(screen_id, partition_id, tmp_data.initLayout(m_format->getNewLayoutId(screen_id, partition_id)));
}
void MainWindow::on_delLayout(int screen_id, int partition_id, int layout_id)
{
    ui->tabWidget->delLayout(screen_id, partition_id, layout_id);
}
void MainWindow::on_addItem(int screen_id, int partition_id, int layout_id)
{
    ui->tabWidget->setCurrentChangeedScreenFromId(screen_id);
    Format tmp_data;
    Item add_item = tmp_data.initItem(m_format->getNewItemId(screen_id, partition_id, layout_id));
    addItem(screen_id, partition_id, layout_id, add_item);
}
void MainWindow::on_delItem(int screen_id, int partition_id, int layout_id, int item_id)
{
    ui->tabWidget->delItem(screen_id, partition_id, layout_id, item_id);
}
void MainWindow::on_addCabPos(int screen_id, int partition_id, int layout_id, int item_id)
{
    ui->tabWidget->setCurrentChangeedScreenFromId(screen_id);

    AddCabPosDialog dialog;
    QPair<QString, QString> pair = dialog.showAndGetResult();
    if (m_format->isCabPosRepeated(screen_id, partition_id, layout_id, item_id, pair.first))
    {
        QMessageBox::warning(this, QObject::tr("waring"), QObject::tr("car number position string repeated"));
        return;
    }
    if (pair.first != "")
    {
        Format tmp_data;
        CabPosItem add_cabpositem = tmp_data.initCabPosItem(pair.second, item_id);
        addCabPosItem(screen_id, partition_id, layout_id, item_id, pair.first, add_cabpositem);
    }
}
void MainWindow::on_delCabPos(int screen_id, int partition_id, int layout_id, int item_id, QString cab_pos_item_str)
{
    ui->tabWidget->delCabPos(screen_id, partition_id, layout_id, item_id, cab_pos_item_str);
}


//辅助函数
void MainWindow::addScreen(Screen add_screen)
{
    ui->treeWidget->addScreenItemChildren(add_screen);
    ui->tabWidget->tabAddScreen(add_screen);
    ui->treeWidget->getAllDataFromTwView();
}
void MainWindow::addPartition(int screen_id, Partition add_partition)
{
    ui->treeWidget->addPartitionItemChildrenUseId(screen_id, add_partition);
    ui->tabWidget->addPartition(screen_id, add_partition);
    ui->treeWidget->getAllDataFromTwView();
}
void MainWindow::addLayout(int screen_id, int partition_id, Layout add_layout)
{
    ui->treeWidget->addLayoutItemChildrenUseId(screen_id, partition_id, add_layout);
    ui->tabWidget->addLayout(screen_id, partition_id, add_layout);
    ui->treeWidget->getAllDataFromTwView();
}
void MainWindow::addItem(int screen_id, int partition_id, int layout_id, Item add_item)
{
    ui->treeWidget->addItemItemChildrenUseId(screen_id, partition_id, layout_id, add_item);
    ui->tabWidget->addItem(screen_id, partition_id, layout_id, add_item);
    ui->treeWidget->getAllDataFromTwView();
}
void MainWindow::addCabPosItem(int screen_id, int partition_id, int layout_id, int item_id, QString cab_pos_item_str, CabPosItem add_cabpositem)
{
    ui->treeWidget->addCabPosItemChildrenUseId(screen_id, partition_id, layout_id, item_id, cab_pos_item_str, add_cabpositem);
    ui->tabWidget->addCabPos(screen_id, partition_id, layout_id, item_id, cab_pos_item_str, add_cabpositem);
    ui->treeWidget->getAllDataFromTwView();
}

void MainWindow::initTreeWidget(Format *format)
{
    ui->treeWidget->createTreeWidget(format);
    connect(ui->treeWidget, SIGNAL(screen_item_clicked(int)), this, SLOT(on_treeScreenSelected(int)));
    connect(ui->treeWidget, SIGNAL(partition_item_clicked(int, int)), this, SLOT(on_treePartitionSelected(int, int)));
    connect(ui->treeWidget, SIGNAL(layout_item_clicked(int, int, int)), this, SLOT(on_treeLayoutSelected(int, int, int)));
    connect(ui->treeWidget, SIGNAL(item_item_clicked(int, int, int, int, QString)), this, SLOT(on_treeItemSelected(int, int, int, int, QString)));

    connect(ui->treeWidget, SIGNAL(add_screen()), this, SLOT(on_addScreen()));
    connect(ui->treeWidget, SIGNAL(del_screen(int)), this, SLOT(on_delScreen(int)));
    connect(ui->treeWidget, SIGNAL(add_partition(int)), this, SLOT(on_addPartition(int)));
    connect(ui->treeWidget, SIGNAL(del_partition(int, int)), this, SLOT(on_delPartition(int, int)));
    connect(ui->treeWidget, SIGNAL(add_layout(int, int)), this, SLOT(on_addLayout(int, int)));
    connect(ui->treeWidget, SIGNAL(del_layout(int, int, int)), this, SLOT(on_delLayout(int, int, int)));
    connect(ui->treeWidget, SIGNAL(add_item(int, int, int)), this, SLOT(on_addItem(int, int, int)));
    connect(ui->treeWidget, SIGNAL(del_item(int, int, int, int)), this, SLOT(on_delItem(int, int, int, int)));
    connect(ui->treeWidget, SIGNAL(add_cabpos(int, int, int, int)), this, SLOT(on_addCabPos(int, int, int, int)));
    connect(ui->treeWidget, SIGNAL(del_cabpos(int, int, int, int, QString)), this, SLOT(on_delCabPos(int, int, int, int, QString)));
    connect(ui->treeWidget, SIGNAL(update_clicked()), this, SLOT(on_updateData()));
}

void MainWindow::initTabWidget(Format *format)
{
    ui->tabWidget->showFormatData(format);
    //注册item和partition控件选中回调函数
    GlobalObserver::getGlobalObserver()->attach(item_select_msg, this, SLOT(on_item_select(int, int, int, int, QString)));
    GlobalObserver::getGlobalObserver()->attach(layout_select_msg, this, SLOT(on_layout_select(int, int, int)));
    GlobalObserver::getGlobalObserver()->attach(partition_select_msg, this, SLOT(on_partition_select(int, int)));
    GlobalObserver::getGlobalObserver()->attach(screen_select_msg, this, SLOT(on_screen_select(int)));
}

//将所有子控件显示为未选择状态
void MainWindow::mousePressEvent(QMouseEvent *event)
{
    Q_UNUSED(event);
    ui->tabWidget->setAllChildrenLossSelect();
}

//按钮 hide之后就不响应shortcut快捷键了,在ui中设置了shortcut，重写keypress相应保存数据槽函数
void MainWindow::keyPressEvent(QKeyEvent *event)
{
    // 按下 ctrl
    if (event->modifiers() == Qt::ControlModifier)
    {
        if (event->key() == Qt::Key_S)
            on_pushButton_save_clicked();
    }
}

void MainWindow::on_updateData()
{
    if (!m_format)
        return;
    ui->treeWidget->getAllDataFromTwView();
    ui->tabWidget->tabUpdateScreens(m_format);
}

void MainWindow::on_action_triggered_help()
{
    m_help_dialog->exec();
}

void MainWindow::on_action_preview_triggered()
{
    if (g_preview)
    {
        ui->action_preview->setText("预览");
        g_preview = false;
    } else
    {
        ui->action_preview->setText("取消预览");
        g_preview = true;
    }
    on_updateData();
}
